package com.yf.exam.modules.trainingarchives.teacherattendance.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import com.yf.exam.common.entity.Result;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.exception.ServiceException;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationDTO;
import com.yf.exam.modules.trainingarchives.organization.vo.OrganizationVO;
import com.yf.exam.modules.trainingarchives.teacherattendance.dto.TeacherAttendanceDTO;
import com.yf.exam.modules.trainingarchives.teacherattendance.entity.TeacherAttendance;
import com.yf.exam.modules.trainingarchives.teacherattendance.service.ITeacherAttendanceService;
import com.yf.exam.modules.trainingarchives.teacherattendance.vo.TeacherAttendanceListVO;
import com.yf.exam.modules.trainingarchives.teacherattendance.vo.TeacherAttendanceVO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.dto.TeachingPlanExecutionDTO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.entity.TeachingPlanExecution;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.vo.TeachingPlanExecutionListVO;
import com.yf.exam.modules.trainingarchives.teacherplanexecution.vo.TeachingPlanExecutionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 授课教师签到表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Api(tags={"教师签到"})
@RestController
@RequestMapping("/org/api/teacher-attendance")
@Slf4j
public class TeacherAttendanceController {

    @Resource
    private ITeacherAttendanceService iTeacherAttendanceService;

    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody TeacherAttendanceDTO reqDTO) {
        try {
            iTeacherAttendanceService.add(reqDTO);
            return Result.ok();
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }

    @ApiOperation(value = "修改")
    @PostMapping (value = "/update")
    public Result update(@RequestBody TeacherAttendanceDTO reqDTO) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "签到id不能为空!");
            //复制参数
            iTeacherAttendanceService.update(reqDTO);
            return Result.ok("修改成功！");
        }catch (ServiceException e){
            return Result.faild(e);
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeacherAttendanceController.delete.baseIdReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkNotNull(baseIdReqDTO.getId(), "签到id不能为空!");
            Boolean result = iTeacherAttendanceService.removeById(baseIdReqDTO.getId());
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TeacherAttendanceController.delete.error:{}", e.getMessage(), e);
            return Result.faild("删除教师签到执行失败!");
        }
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public Result<IPage<TeacherAttendanceListVO>> paging(@RequestBody PagingReqDTO<TeacherAttendanceDTO> reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeachingPlanExecutionController.paging.dto:{}", JSON.toJSONString(reqDTO));
            }
            //分页查询并转换
            IPage<TeacherAttendanceListVO> page = iTeacherAttendanceService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            log.error("TeachingPlanExecutionController.paging.error:{}", e.getMessage(), e);
            return Result.faild("查询失败");
        }
    }

    @ApiOperation(value = "查询详情")
    @PostMapping (value = "/get")
    public Result<TeacherAttendanceVO> get(@RequestBody BaseIdReqDTO reqDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getId()), "教学计划执行id不能为空!");
        TeacherAttendance teacherAttendance = iTeacherAttendanceService.getById(reqDTO.getId());
        if (teacherAttendance == null) {
            return Result.faild("教师签到不存在!");
        }
        TeacherAttendanceVO vo = new TeacherAttendanceVO();
        BeanUtils.copyProperties(teacherAttendance,vo);
        return Result.ok(vo);
    }



}
